import Taro from '@tarojs/taro'

const getBaseUrl = () => {
  let BASE_URL = '';

  if (process.env.NODE_ENV === 'development') {
    // 开发环境 - 根据请求不同返回不同的BASE_URL
    BASE_URL = 'http://ih9nb2.natappfree.cc'
  } else {
    // 生产环境

    BASE_URL = 'https://*********'

  }
  return BASE_URL
}

export default function http({ url = '', data = {}, method = 'GET', header = {} }) {
  const BASE_URL = getBaseUrl();
  // let contentType = "application/x-www-form-urlencoded";
  let contentType = "application/json;charset=UTF-8";
  // contentType = params.contentType || contentType;

  const option = {
    url: BASE_URL + url,  //地址
    data,   // 传参
    method, // 请求方式，默认 GET
    timeout: 50000, // 超时时间
    header: {  // 请求头
      'content-type': contentType,
      'Authorization': Taro.getStorageSync('Authorization'),
      ...header
    }
  };
  return Taro.request(option);
}

export const uploadHttp = ({ url = '', filePath = '', name = '', formData = {}, ...otherParams }) => {
  const BASE_URL = getBaseUrl();

  const option = {
    url: BASE_URL + url,
    filePath,
    name,
    formData,
    ...otherParams
  }

  return Taro.uploadFile(option)
}